×
☰ See All Chapters

How to locate element by LinkText and PartialLinkText locator in selenium

Out of all the locators, linkText and partialLinkText are used to identify only the links present on the webpage. (Elements whose tagname is “a” and are called as anchor tags). LinkText locator should be used when the text of the link is constant. partialLinkText locator should be used when certain part of the link text is getting changed every time the page is loaded. i.e. for partially dynamically changing text, we use partialLinkText locator. To handle those elements whose text changes completely, we can’t use partialLInkText locator. It should be handled by “xpath” locator. If we use try to use these 2 locators on other type of elements (except Links/ anchor tags), then we get “NoSuchElementException”.

Syntax:

linkText=text value of anchor tag

partialLinkText=partial text value of anchor tag

Example:

<html>

</head>

<body>

        <form name="signup" id="form" modelAttribute="data">

                <table align="center" width=90% cellspacing="2" cellpadding="2">

                        <tr>

                                <td><a href="#">Welcome to www.tools4testing.com</a></td>

                        </tr>

                        <tr>

                                <td><a href="#">Welcome to www.java4coding.com</a></td>

                        </tr>

                </table>

        </form>

</body>

</html>

linkText=Welcome to www.tools4testing.com

partialLinkText=java

You can write the script and test these using our Test Page

selenium-link-text-partial-link-text-locator-0
 
selenium-link-text-partial-link-text-locator-1
 

All Chapters
Author